<template >
  <basiccard :header_nav="header_nav">
    <el-row>
      <el-col>
        <el-button type="primary" @click="addCateForm">添加分类</el-button>
      </el-col>
    </el-row>
    <tree-table
      class="TreeTable"
      :data="cateList"
      :columns="columns"
      :selection-type="false"
      :expand-type="false"
      :show-index="true"
      index-text="#"
      border
    >
      <template slot="isok" slot-scope="scope">
        <i class="el-icon-success" style="color:blue" v-if="scope.row.cat_deleted ===false"></i>
        <i class="el-icon-error" v-else style="color:red"></i>
      </template>
      <template slot="order" slot-scope="scope">
        <el-tag type size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
        <el-tag type="success" size="mini" v-else-if="scope.row.cat_level===1">二级</el-tag>
        <el-tag type="warning" size="mini" v-else>三级</el-tag>
      </template>
      <template slot="operate" slot-scope="scope">
        <el-button type="success" size="mini" @click>编辑</el-button>
        <el-button type="danger" size="mini" @click>删除</el-button>
      </template>
    </tree-table>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="params.pagenum"
      :page-sizes="[3, 5, 8, 10]"
      :page-size="params.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 添加分类模态框 -->
    <el-dialog title="添加分类" :visible.sync="addCateVisible" width="50%" @close="addCateClose">
      <el-form ref="addCateFormRef" :model="cateForm" label-width="80px" :rules="addCateRules">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="cateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            :clearable="true"
            v-model="getParentIds"
            :options="getParentCate"
            expand-trigger="hover"
            :props="cascaderProps"
            :change-on-select="true"
            @change="casChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </basiccard>
</template>
<script>
export default {
  name: "cate",
  data() {
    return {
      header_nav: {
        path: "/welecome",
        rolesMge: "商品管理",
        rolesList: "商品列表"
      },
      addCateVisible: false,
      cateForm: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0
      },
      addCateRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" }
        ]
      },
      getParentCate: [],
      getParentIds: [],
      // 级联配置对象

      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children"
      },
      params: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      cateList: [],
      total: 0,
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          type: "template",
          template: "isok"
        },
        {
          label: "排序",
          type: "template",
          template: "order"
        },
        {
          label: "操作",
          type: "template",
          template: "operate"
        }
      ]
    };
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.params
      });
      if (res.meta.status !== 200)
        return this.$message.error("获取商品分类列表数据失败");
      this.cateList = res.data.result;
      this.total = res.data.total;
      console.log(res.data);
    },
    handleSizeChange(val) {
      this.params.pagesize = val;
      this.getCateList();
    },
    handleCurrentChange(val) {
      this.params.pagenum = val;
      this.getCateList();
    },
    addCateForm() {
      this.getParentCateMt();
      //              console.log(222);

      this.addCateVisible = true;
    },
    async getParentCateMt() {
      const { data: res } = await this.$http.get("categories", {
        params: {
          type: 2
        }
      });
      if (res.meta.status !== 200) return this.$message.error("获取列表失败");
      this.getParentCate = res.data;
      console.log(res.data);
    },
    casChange() {
      
      console.log(this.getParentIds);
      if(this.getParentIds.length>0) {
        this.cateForm.cat_pid = this.getParentIds[this.getParentIds.length-1]
        this.cateForm.cat_level = this.getParentIds.length
        return
      }else {
        this.cateForm.cat_pid = 0
        this.cateForm.cat_level = 0
      }
      
    },
    addCateClose() {
      this.cateForm.cat_name = ''
      this.cateForm.cat_pid = 0
      this.cateForm.cat_level = 0
      this.getParentIds = []
    },
    addCate() {
    console.log(this.cateForm);
    this.$refs.addCateFormRef.validate(async valid=>{
      if(!valid) return
      const {data:res}  = await this.$http.post('categories',this.cateForm)
      if(res.meta.status!==201) return this.$message.error('添加分类失败')
      this.getCateList()
      this.$message.success('添加分类成功');
      this.addCateVisible = false
      
    } )
    
    }
  },
  created() {
    this.getCateList();
  }
};
</script>
<style lang="scss" scoped>
.TreeTable {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>